package org.jeecgframework.web.service.impl;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.service.impl.CommonServiceImpl;
import org.jeecgframework.core.util.oConvertUtils;
import org.jeecgframework.web.service.UploadInstallRecordService;
import org.jeecgframework.web.service.ZyCommonService;
import org.jeecgframework.web.system.pojo.base.ZyFileLogEntity;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.web.util.ExcelUtil;
import org.jeecgframework.web.util.FileBean;
import org.jeecgframework.web.util.StaticValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 
 * @ClassName：UploadInstallRecordServiceImpl.java
 * @Description：上传安装记录
 * @author：LeiYoung
 * @version：V1.0
 * @Date：2016年9月29日 下午3:46:15
 */
@Service("uploadInstallRecordService")
@Transactional
public class UploadInstallRecordServiceImpl extends CommonServiceImpl implements UploadInstallRecordService {
	@Autowired
	private SystemService systemService;

	@Autowired
	private ZyCommonService zyCommonService;

	/**
	 * 导入Excel到表zy_file_log
	 * 
	 * @Description:
	 * @author LeiYoung
	 * @Date 2016年9月29日 下午3:33:59
	 */
	@Override
	public ZyFileLogEntity doSaveZyFileLogEntity(FileBean fileBean) throws Exception {
		/** 文件管理 */
		ZyFileLogEntity fileLog = new ZyFileLogEntity();

		try {
			/** 业务类型：定期导入安装记录 */
			fileLog.setBusinessType(StaticValue.IMPORT_FILE_INTALL_RECORDER);

			/** 旧文件名 */
			fileLog.setOldFileName(fileBean.getFileRealName());

			/** 新文件名 */
			fileLog.setNewFileName(fileBean.getFileName());

			/** 路径 */
			fileLog.setFilePath(fileBean.getSavePath());

			/** 文件类型 */
			fileLog.setFileType(StaticValue.DIC_FILE_TYPE_XLSX);

			/** 文件状态 */
			fileLog.setStatus(StaticValue.DIC_TRUE_OR_FALSE_NUM_0);

			/** 执行添加 */
			systemService.save(fileLog);

		} catch (Exception e) {

			e.printStackTrace();

			throw e;

		}

		return fileLog;
	}

	/**
	 * 
	 * @Description:
	 * @author LeiYoung
	 * @Date 2016年9月29日 下午4:06:30
	 */
	@Override
	public AjaxJson checkExcelSn(HttpServletRequest request) throws Exception {

		System.out.println("进入 导入安装记录 检查SN Action");

		AjaxJson j = new AjaxJson();

		// 开始时间
		long begin = System.currentTimeMillis();

		boolean flag = true;

		// 消息
		String message = "导入成功";
		try {
			// 查找为处理的差异数据
			String sql = "select count(1) as total from zy_install_record_cp t where t.status = '" + StaticValue.DIC_TRUE_OR_FALSE_NUM_0 + "'";

			// 返回总数
			int total = zyCommonService.getCountBySql(sql);

			if (total == 0) {
				// 总数不等于 0

				// 判断是否存在空的SN
				boolean hasNull = this.isEmptyBySn(request);

				if (!hasNull) {
					message = "序列号SN存在空值";
				}

			} else {
				// 获取去掉空格的fileLogId
				String fileLogId = oConvertUtils.getString(request.getParameter("fileLogId"));
				
				// 根据fileLogId查询一个实体
				ZyFileLogEntity fileLog = systemService.getEntity(ZyFileLogEntity.class, fileLogId);
				
				// 如过有差异数据未处理，此处的
				// 上传记录应该删除，否则定时里会查出来并解析
				systemService.delete(fileLog);
				
				flag = false;
				
				message = "请先处理差异数据";

			}

		} catch (Exception e) {
			
			j.setSuccess(false);
			j.setMsg(e.getMessage());
			e.printStackTrace();
			
		}
		
		long end = System.currentTimeMillis();
		System.out.println("安装记录Excel 检验完毕，用时：" + (end - begin) / 1000 + "." + (end - begin) % 1000 + "秒");
		j.setSuccess(flag);
		j.setMsg(message);
		return j;
	}

	/**
	 * 
	 * @Title：isEmptyBySn
	 * @Description：判断是否存在空的SN
	 * @author：LeiYoung
	 * @Date：2016年9月29日 下午5:10:52
	 * @param req
	 * @return boolean
	 * @throws Exception
	 */
	private boolean isEmptyBySn(HttpServletRequest req) throws Exception {

		// 获取文件路径
		String filePath = req.getParameter("filePath");

		// 解析Excel
		List<Map<String, Object>> mapList = ExcelUtil.analyzeExcel(filePath, "1");

		// 遍历List
		for (Map<String, Object> map : mapList) {
			// 获取第三列的值
			Object sn = map.get("2");
			
			if (sn == null || sn.equals("") || sn.toString().trim().length() <= 0) {
				// 非空判断

				return false;
			}
		}
		return true;
	}

	@Override
	public String checkSnRepeat(List<String> snList, int begin, int end) throws Exception {
		System.out.println("beginR:" + begin);
		System.out.println("endR:" + end);
		for (int i = begin; i <= end; i++) {
			if (snList.get(i).length() != 7 || snList.get(i).length() != 10) {
				return snList.get(i);
			}
		}
		return null;
	}
}